package com.homegoods.orderinfodetail.presenter;

import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.orderinfodetail.OrderInfoApiUtils;
import com.homegoods.orderinfodetail.bean.ShoppingOrderdetail;
import com.homegoods.orderinfodetail.model.ComdiyBean;
import com.homegoods.orderinfodetail.model.OrderInFoShopBean;
import com.homegoods.orderinfodetail.model.PostOrderGoodBean;
import com.homegoods.orderinfodetail.ui.view.ConfirmOrderView;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.mvp.BaseView;
import com.kedll.library.util.CollectionUtils;
import com.kedll.library.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;

public class ConfirmOrderPresenter extends BasePresenter<ConfirmOrderView> {
    private MultiTypeAdapter adapter = new MultiTypeAdapter();

    private List<OrderInFoShopBean> mListGoods;
    private ShoppingOrderdetail shoppingOrderdetail;

    public ConfirmOrderPresenter(ConfirmOrderView view) {
        super(view);
    }

    public MultiTypeAdapter getAdapter() {
        return adapter;
    }

    public void setListData(String content) {
        //获取确认订单信息详情数据
        OrderInfoApiUtils.getPrepareOrder(content, new Consumer<ShoppingOrderdetail>() {
            @Override
            public void accept(ShoppingOrderdetail shoppingOrderdetail) throws Exception {
                mView.showAddress(shoppingOrderdetail.getAddress());//将地址数据显示出来
                mListGoods = shoppingOrderdetail.getFactory();
                for (OrderInFoShopBean cartBean : mListGoods) {//循环添加商品数据
                    if (CollectionUtils.isEmpty(cartBean.getComdiylist())) {
                        continue;
                    }
                    adapter.addItem(cartBean.createItem(adapter));  //店铺数据
                    for (ComdiyBean comdiy : cartBean.getComdiylist()) {    //商品数据
                        adapter.addItem(comdiy.createItem(adapter));
                    }
                }
                adapter.notifyDataSetChanged();
            }
        }, throwable -> {

        });

    }

    /**
     * sid:'商品sid',serve:'上门服务',num:'商品数量',stadarry:'属性sid集合,传字符数组',argument:'属性的中文名称'
     *
     * @return
     */
    private List<PostOrderGoodBean> addPostOrderGood() {
        if (mListGoods == null && mListGoods.size() <= 0) {
            return null;
        }
        List<PostOrderGoodBean> beanList = new ArrayList<>();
        for (OrderInFoShopBean bean : mListGoods) {
            for (ComdiyBean good : bean.getComdiylist()) {
                List<PostOrderGoodBean> postlist = JSON.parseArray(good.getData(), PostOrderGoodBean.class);
                PostOrderGoodBean postBean = postlist.get(0);
                postBean.setNum(good.getComdiynum());
                beanList.add(postBean);
            }
        }
        return beanList;
    }

    /**
     * 添加订单前获取的订单详情
     */
    public void getOrderDetails(int addresid, String price) {
        String content = JSON.toJSONString(addPostOrderGood());
        if (TextUtils.isEmpty(content)) {
            return;
        }
        OrderInfoApiUtils.getOrderDetails(content, addresid, orderPayBean -> {
            ARouter.getInstance()
                    .build(ARouterPath.PATH_MINE_ORDER_PAY)
                    .withString("addreSid", orderPayBean.getAddress().getSid())
                    .withString("content", orderPayBean.getContent())
                    .withString("price", price)
                    .navigation();
        }, throwable -> {

        });
    }

}